<template>
  <el-form :model="nodeModalForm" ref="nodeModal" label-placement="left" label-width="120px" :rules="nodeModalRules"
    size="small">
    <el-row :gutter="20">
      <el-col :span="24">
        <el-form-item label="节点名称" prop="nodeName">
          <el-input v-model.trim="nodeModalForm.nodeName"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="新建列名称" prop="rowName">
          <el-input v-model.trim="nodeModalForm.rowName"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="列数据" prop="separator">
          <el-input v-model.trim="nodeModalForm.value"></el-input>
        </el-form-item>
      </el-col>
    </el-row>
    <div class="nodeModal-footer">
      <span class="dialog-footer">
        <el-button plain @click="handleConfigCancel">取消</el-button>
        <el-button type="primary" @click="handleConfigConfirm">更新</el-button>
      </span>
    </div>
  </el-form>
</template>

<script>
import { computed, defineComponent, inject, onMounted, ref } from "vue";
import { useEtlStore } from "../../../../../store/flowEtl";

export default defineComponent({
  name: "node-config-const",
  props: {
    node: Object,
  },
  components: {},
  setup(props, ctx) {
    const etlStore = useEtlStore();
    const nodeList = computed(() => etlStore.nodeList);
    const nodeConfigClose = inject("nodeConfigClose");
    const nodeModalForm = ref({
      nodeName: "",
      rowName: "",
      value: "",
      fields: [],
    });
    const nodeModal = ref();
    const nodeModalRules = ref({
      nodeName: {
        required: true,
        message: "请输入数据源名称",
        trigger: "blur",
      },
      rowName: {
        required: true,
        message: "请输入列名称",
        trigger: "blur",
      },
      value: {
        required: true,
        message: "请输入常量",
        trigger: "blur",
      },
    });
    const handleConfigCancel = () => {
      nodeConfigClose();
    };
    const nodeConfigUpdate = (node) => {
      let findAlias = nodeModalForm.value.fields.find((item) => {
        return item.alias === nodeModalForm.value.rowName;
      });
      if (findAlias) {
        ElMessage({
          type: "warning",
          message: "列名称已存在。",
        });
        return;
      }
      nodeModalForm.value.fields.push({
        COLUMN_COMMENT: node.rowName,
        COLUMN_NAME: node.rowName,
        DATA_TYPE: "int",
        alias: node.rowName,
      });
      let nodes = [...nodeList.value].map((item) => {
        if (item.id === node.id) {
          item.status = true;
          item.nodeName = node.nodeName;
          item.config.con.rowName = node.rowName;
          item.config.con.value = node.value;
          item.config.fields = node.fields;
        }
        return item;
      });
      etlStore.setNodeList(nodes);
      nodeConfigClose();
    };
    const handleConfigConfirm = () => {
      nodeModal.value.validate((valid) => {
        if (valid) {
          nodeConfigUpdate(nodeModalForm.value);
        } else {
          return false;
        }
      });
    };
    const resetForm = () => {
      nodeModalForm.value = {
        id: null,
        nodeName: "",
        rowName: "",
        value: "",
        fields: [],
      };
    };
    onMounted(() => {
      resetForm();
      nodeModalForm.value = {
        id: props.node.id,
        nodeName: props.node.nodeName,
        rowName: props.node.config.con.rowName,
        value: props.node.config.con.value,
        fields: props.node.config.fields || [],
      };
    });
    return {
      nodeList,
      nodeModalForm,
      nodeModalRules,
      nodeModal,
      resetForm,
      handleConfigCancel,
      handleConfigConfirm,
      nodeConfigUpdate,
      nodeConfigClose,
    };
  },
});
</script>

<style scoped>
.nodeModal-footer {
  padding: var(--el-dialog-padding-primary);
  padding-top: 10px;
  text-align: right;
  box-sizing: border-box;
}
</style>